package com.mlkj.serve.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.sd4324530.fastweixin.api.MenuAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.util.NetWorkCenter;
import com.mlkj.serve.util.SystemContent;

/**
 * @author hanchen
 * 
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController {
	private static final Logger log = LoggerFactory
			.getLogger(MenuController.class);

	@RequestMapping(value = "show")
	public String showMenuSetting() {
		return "setmenu";
	}

	@RequestMapping(value = "creat")
	public String creatMenu(HttpServletRequest request,
			HttpServletResponse response) {
		// ------------------------fastweixin的bug以下代码暂时不能使用--------------------------
//		log.info("log创建菜单");
//		String mainName = request.getParameter("firstmenu");
//		String subName = request.getParameter("childMenuName");
//		String subUrl = request.getParameter("childMenUrl");
//		log.info(mainName + "  " + subName + "  " + subUrl);
//		ApiConfig config = new ApiConfig(SystemContent.APPID,
//				SystemContent.SECRET);
//		 MenuAPI menuAPI = new MenuAPI(config);
//		MenuApi menuApi = new MenuApi(config);
//		menuApi.deleteMenu();
//		Menu menu = new Menu();
//		MenuButton main1 = new MenuButton();
//		main1.setType(MenuType.CLICK);
//		main1.setKey("menu");
//		main1.setName(mainName);
//		log.info("主菜单:" + main1.toJsonString());
//		// 准备子菜单
//		MenuButton sub1 = new MenuButton();
//		sub1.setKey("sub1");
//		sub1.setName(subName);
//		sub1.setType(MenuType.VIEW);
//		sub1.setUrl(subUrl);
//		log.info("子菜单:" + sub1.toJsonString());
//		MenuButton sub2 = new MenuButton();
//		sub1.setKey("sub2");
//		sub1.setName("百度");
//		sub1.setType(MenuType.VIEW);
//		sub1.setUrl("http://www.baidu.com");
//
//		List<MenuButton> list = new ArrayList<MenuButton>();
//		list.add(sub1);
//		list.add(sub2);
//		// 将子菜单放入主菜单里
//		main1.setSub_button(list);
//		log.info("加入子菜单后" + main1.toJsonString());
//		List<MenuButton> mainList = new ArrayList<MenuButton>();
//		mainList.add(main1);
//		// 将主菜单加入请求对象
//		menu.setButton(mainList);
//		// 创建菜单
//		log.info(menu.toJsonString());
//		ResultType resultType = menuApi.createMenu(menu);
//		log.info(resultType.toString());
		// ------------------------fastweixin的bug--------------------------
		ApiConfig config = new ApiConfig(SystemContent.APPID,
				SystemContent.SECRET);
		MenuAPI menuAPI = new MenuAPI(config);
		menuAPI.deleteMenu();
		String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=0vH9HzRaKHGWyvWnvkiNr_pAQcqD0h2lKNnx3UMfxhMovBX4_Cmka69fF3pW8RA4Ec-C6jwSAaWHHER_dEZg3Bk6dhDnff3LcSTQj_M6Ex4";
		String json = "{\"button\":[{\"name\":\"OAuth测试\",\"sub_button\":[{\"type\":\"view\",\"name\":\"测试\",\"url\":\"http://servicewechat.ngrok.com/servicewechat/oauth/user\"}]}]}";
		log.info("JSON"+json);
		BaseResponse response2 = NetWorkCenter.post(url, json);
		log.info("errmsg"+response2.getErrmsg());
		return "creatResult";
	}

}
